var centerClientX = 118, centerClientY = 320;
/**
 * значение r
 */
var clientR = 80;
/**
 * выбранное значение R
 */
function getR() {
    var r = checkboxValueByClass('setR');
    return r.valid ? r.value : 1;
}
function bodyReady(imagesFolder) {
    var el = getByClass('response').item(0);
    el.scrollTop = el.scrollHeight;
    
    redImg = imagesFolder + 'red.png';
    blueImg = imagesFolder + 'blue.png';
    
    showPoints();
}
var redImg;
var blueImg;
function clickCheckBoxHandler(el, clazz) {
    var arr = getByClass(clazz);
    for (var i = 0; i < arr.length; ++ i) {
        arr.item(i).checked = false;
    }
    if (clazz == 'setR') {
        setTimeout(showPoints, 100);
    }
    el.checked = true;
}
function showPoints() {
    var t = '';
    points.forEach(function(p) {
        t += showPoint(p.x, p.y);
    });
    getById('points').innerHTML = t;
}
function showPoint(x, y){
    var r = getR();
    if (Math.abs(x) <= r && Math.abs(y) <= r) {
        var type = checkPointInArea(x, y, r) ? 0 : 1 ;
        return showForm(centerClientX + x*clientR/getR()-2.5, centerClientY - y*clientR/getR()-2.5, type);
    }
    else {
        return '';
    }
}
function showForm(posX, posY, type) {
    var out = "";
    out += "<div style='";
    out += "position: absolute; left: "+posX+"px; top: "+posY+"px; width: 5px; height: 5px;'>";
    out += "<img src='"+(type ? blueImg : redImg)+"' width='5px' height='5px' />";
    out += "</div>";
    return out;
}
function inInterval(x, x1, x2) {
    return x1 <= x && x <= x2;
}
function inBox(x, y, x1, y1, x2, y2) {
    return inInterval(x, x1, x2) && inInterval(y, y1, y2);
}
function checkPointInArea(x, y, r) {
    return  inBox (x, y, -r, 0, 0, r) ||
            (inBox (x, y, 0, 0, r, r) && x*x + y*y <= r*r) ||
            (inBox (x, y, -r, -r/2, 0, 0) && y >= -x/2 - r/2);
}